<?php

class Controller_Index extends Controller_TemplateNew
{
    public $_section;
    
	public function  before() {
		parent::before();
		$this->db = Database::instance();
		$this->mod = Model::factory('directory');
		$this->section = 'Home - ';
                
                $this->_section = 'index';

		$this->template->description = $this->settings['description'];
		$this->template->keywords = $this->settings['keywords'];
		$this->template->linkSettings = $this->settings;
                
                $this->template->header->active='index';
                
	}
    
	public function actionIndex()
	{
		$this->template->title = $this->section.$this->settings['title'];
		$this->template->content = new View('index/index');
                $this->template->content->bottomblock = new View('index/bottomblock');
                $this->template->content->downblock = new View('index/downblock');
                $this->template->content->downblock->formIdea = $this->formIdea;

                //list of catalog
                $signList = $this->mod->getCategories('catalog', 'position');
                $this->template->content->bottomblock->signList = $signList;
                
                //Popular signs
                $mostPopular = $this->mod->getItemsCheckbox1('catalog','position');
                foreach ($mostPopular as $k=>$v) {
                    $mostPopularImg = $this->mod->getItemImages($k,'catalog');
                    if (isset($mostPopularImg)) {
                        foreach ($mostPopularImg as $key=>$value) {
                            $mostPopular[$k]['catalogImage'] = $this->mod->imagePath($value,'sign');
                        }
                    }
                }
//                echo '<pre>';
//                print_r ($mostPopular);exit;
                $this->template->content->bottomblock->mostPopular = $mostPopular;
                
                $mainBlock = $this->mod->getItem(91, $this->_section);
                $this->template->content->mainBlock = $mainBlock;
                $centrBlock = $this->mod->getItem(92, $this->_section);
                $this->template->content->centrBlock = $centrBlock;
                
                //Right Block
                $rightBlock = $this->mod->getItems('images');
                $indexPageBlockCategory = $this->mod->getCategoryByAlias('images-56','images');
                $rightBlockItems = $this->mod->getCategoryItems($indexPageBlockCategory['id'],'images');
                $tempImg = $this->mod->getImagesByItem('images');
                $tempAtt = $this->mod->getAttachmentsByItem('images');
                foreach ($tempImg as $v) {
                    $rightBlock[$v[0]['itemId']]['box3'] = $this->mod->imagePath($v[0],'box3');
                }
                foreach ($tempAtt as $v) {
                    $rightBlock[$v[0]['itemId']]['attacment'] = $this->mod->attachmentPath($v[0]);
                }
                
                $bottomBlockCategory = $this->mod->getCategoryByAlias('images-79','images');
                $formConfig = $this->mod->getItemByAlias('9','images');
                $this->template->content->downblock->showForm = ($formConfig['checkbox3']==1) ? true : false;
                $bottomBlockItems = $this->mod->getCategoryItems($bottomBlockCategory['id'],'images','position');
                $tempImg = $this->mod->getImagesByItem('images','position');
                $tempAtt = $this->mod->getAttachmentsByItem('images');
                foreach ($tempImg as $v) {
                    $bottomBlockItems[$v[0]['itemId']]['box4'] = $this->mod->imagePath($v[0],'box4');
                }
                foreach ($tempAtt as $v) {
                    $bottomBlockItems[$v[0]['itemId']]['attacment'] = $this->mod->attachmentPath($v[0]);
                }
                $this->template->content->downblock->items = $bottomBlockItems;
                
//                print_r ($bottomBlockItems);exit;
//                print_r ($rightBlock);exit;
//                $rightBlock = $this->mod->getItem(93, $this->_section);
                $this->template->content->rightBlock = $rightBlock;
                $this->template->content->rightBlockItems = $rightBlockItems;
                
                //
                $bottomBlock = $this->mod->getItem(95, $this->_section);
                $this->template->content->downblock->bottomBlock = $bottomBlock;
                
                $signsList = $this->mod->getCategories($this->_section, 'position');
                $this->template->content->signList = $signsList;
                
                $allImages = $this->mod->getImagesByItem($this->_section);
                if (isset($allImages[$mainBlock['id']])) {
                    foreach ($allImages[$mainBlock['id']] as $key=>$value) {
                        $allImages[$mainBlock['id']][$key]['imgPath'] = $this->mod->imagePath($value,'box1');
                    }
                }
                if (isset($allImages[$centrBlock['id']])) {
                    foreach ($allImages[$centrBlock['id']] as $key=>$value) {
                        $allImages[$centrBlock['id']][$key]['imgPath'] = $this->mod->imagePath($value,'box2');
                    }
                }
//                if (isset($allImages[$rightBlock['id']])) {
//                    foreach ($allImages[$rightBlock['id']] as $key=>$value) {
//                        $allImages[$rightBlock['id']][$key]['imgPath'] = $this->mod->imagePath($value,'box3');
//                    }
//                }
                //Bottom block
                if (isset($allImages[$bottomBlock['id']])) {
                    foreach ($allImages[$bottomBlock['id']] as $key=>$value) {
                        $allImages[$bottomBlock['id']][$key]['imgPath'] = $this->mod->imagePath($value,'box4');
                    }
                }
                
                if (isset($allImages)) {
                    $this->template->content->allImages = $allImages;
                    $this->template->content->bottomblock->allImages = $allImages;
                    $this->template->content->downblock->allImages = $allImages;
                }
                
                
                $this->addScript('/scripts/jquerytools/jquery.rotate.js');
                if ($this->neadRenew) {
                    $this->template = new View('browser');
                }

//                $this->addStyle('/styles/anythingslider/slider.css');
	}
        
        public function actionSendidea() {
            $this->autoRender = false;
            $this->response->setHeader('Content-Type', 'application/json');
            if (Request::$method == 'POST' && Request::$isAjax) {
                if ($this->formIdea->validate()) {
                    $values = $this->formIdea->getValues();
                    $msg =  '';
                    $msg .= 'You have new message from:' . "\n\n";
                    $msg .= ($values['email']!='')?$values['email']:' no email '. ': \n';
                    $msg .= 'My signs idea will be:' . "\n\n";
                    $msg .= $values['idea'];


                    $recipient = $this->settings['email'];
                    $rez = Mailer::sendMail($recipient, 'Message from ' . $this->settings['title']. ' (sign idea), ' .
                            $_SERVER['HTTP_HOST'], $msg, 'noreply@' . $_SERVER['HTTP_HOST']);
                    $this->response->body = json_encode(array('result'=>true));
                }
            } else {
                $this->response->body = json_encode(array('result'=>false));
            }

            
        }
}